package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.DispatchBiz;
import com.hst.capacity.application.biz.ExpressOrderBiz;
import com.hst.capacity.application.biz.IntercityOrderBiz;
import com.hst.capacity.application.biz.OrderPoolBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  调度模块Controller
 *  站点接驳
 *  跨城约车
 *
 * */
@RequestMapping("/dispatch")
@RestController
public class DispatchController {

    @Autowired
    private DispatchBiz dispatchBiz;
    @Autowired
    private IntercityOrderBiz icOrderControlBiz;
    @Autowired
    private ExpressOrderBiz expressControlBiz;
    @Autowired
    private OrderPoolBiz orderPoolBiz;


    /**
     *获取地区下的车站列表
     * @param param
     * @return
     */
    @PostMapping("/findStationList")
    private DefaultVO findStationList(@RequestBody @Valid DispatchParam param) {
        if (param.getAppointmentTimeFrom()==null || param.getAppointmentTimeTo()==null) {
            return success(dispatchBiz.getDispatchStationByRegion(param.getRegionCode(),param.getIdStation()));
        } else {
            return success(dispatchBiz.findStationList(param));
        }
    }

    /**
     *获取选定司机状态
     * 司机信息，订单信息
     * @param param
     * @return
     */
    @PostMapping("/stp/task/driver")
    private DefaultVO getExDriverTaskInfo(@RequestBody @Valid Map<String,String> param){
        String idDriver = param.get("idDriver");
        if (StrUtil.isEmpty(idDriver))
            throw new BusinessException(4001,"司机ID[idDriver]不能为空");
        return success(dispatchBiz.getExDriverTaskInfo(idDriver));
    }

    /**
     *获取地区下的线路列表
     * @param param
     * @return
     */
    @PostMapping("/findCityLineList")
    private DefaultVO findCityLineList(@RequestBody @Valid DispatchParam param) {
        if (param.getAppointmentTimeFrom()==null || param.getAppointmentTimeTo()==null){
            return success(dispatchBiz.getCityLineByRegion(param.getRegionCode(), param.getIdCityLine()));
        }else{
            return success(dispatchBiz.findCityLineList(param));
        }
    }

    /**
     *获取选定司机状态
     * 司机信息，订单信息
     * @param param
     * @return
     */
    @PostMapping("/ic/task/driver")
    private DefaultVO getIcDriverTaskInfo(@RequestBody @Valid Map<String,String> param){
        String idDriver = param.get("idDriver");
        if (StrUtil.isEmpty(idDriver))
            throw new BusinessException(4001,"司机ID[idDriver]不能为空");
        return success(dispatchBiz.getIcDriverTaskInfo(idDriver));
    }

    /**
     * 司机派单
     * @param param
     * @return
     */
    @PostMapping("/driver")
    private DefaultVO DispatchDriver(@RequestBody @Valid DispatchDriverParam param){

        if (param.getIdOrder().startsWith("CJ")){
            return success(icOrderControlBiz.dispatchDriver(param));
        }else if (param.getIdOrder().startsWith("SN")){
            return success(expressControlBiz.dispatchDriver(param));
        }else if (param.getIdOrder().startsWith("ZC")){
            return success(orderPoolBiz.dispatchDriver(param));
        }
        return (success(false));
    }

    /**
     * 司机派单
     * @param param
     * @return
     */
    @PostMapping("/driver/cancel")
    private DefaultVO UnDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        if (param.getIdOrder().startsWith("CJ")){
            return success(icOrderControlBiz.unDispatchDriver(param));
        }else if (param.getIdOrder().startsWith("SN")){
            return success(expressControlBiz.unDispatchDriver(param));
        }
        return (success(false));
    }

    /**
     * 刷新司机状态
     * @param param
     * @return
     */
    @PostMapping("/driver/refresh")
    private DefaultVO ResetDriverTask(@RequestBody @Valid Map<String,String> param){
        String idDriver = param.get("idDriver");
        if (StrUtil.isEmpty(idDriver))
            throw new BusinessException(4001,"司机ID[idDriver]不能为空");
        return success(dispatchBiz.refreshDriverTaskInfo(idDriver));
    }

    /**
     * 取消订单
     * @param param
     * @return
     */
    @PostMapping("/cancel/order")
    private DefaultVO cancelOrder(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        if (idOrder.startsWith("CJ")){
            return success(icOrderControlBiz.cancelOrder(idOrder));
        }else if (idOrder.startsWith("SN")){
            return success(expressControlBiz.cancelOrder(idOrder,param.get("dispatcher")));
        }else if (idOrder.startsWith("ZC")){
            orderPoolBiz.cancelPoolOrder(idOrder);
            return success(MapUtil.builder("idOrder","订单已删除成功").build());
        }
        return (success(false));
    }

    /**
     * 根据城市编码获取城市信息
     * @param
     * @return
     */
    @PostMapping("/reminder")
    private DefaultVO dispatchReminder(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"加盟商ID[idFranchisee]不能为空");
        return success(dispatchBiz.dispatchReminder(idFranchisee));
    }

}
